{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 线性回归"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "VIF 测试 - 多重共线性， VIF值越接近于1，多重共线性越轻，反之越重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import statsmodels.api as sm\n",
    "from statsmodels.stats.outliers_influence import variance_inflation_factor\n",
    "\n",
    "# 宽表\n",
    "data = pd.DataFrame([[15.9,16.4,19,19.1,18.8,20.4,22.7,26.5,28.1,27.6,26.3]\n",
    "                     ,[149.3,161.2,171.5,175.5,180.8,190.7,202.1,212.1,226.1,231.9,239]\n",
    "                     ,[4.2,4.1,3.1,3.1,1.1,2.2,2.1,5.6,5,5.1,0.7]\n",
    "                     ,[108.1,114.8,123.2,126.9,132.1,137.7,146,154.1,162.3,164.3,167.6]]).T\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        1    2      3  4\n",
      "0   149.3  4.2  108.1  1\n",
      "1   161.2  4.1  114.8  1\n",
      "2   171.5  3.1  123.2  1\n",
      "3   175.5  3.1  126.9  1\n",
      "4   180.8  1.1  132.1  1\n",
      "5   190.7  2.2  137.7  1\n",
      "6   202.1  2.1  146.0  1\n",
      "7   212.1  5.6  154.1  1\n",
      "8   226.1  5.0  162.3  1\n",
      "9   231.9  5.1  164.3  1\n",
      "10  239.0  0.7  167.6  1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1.023478872590032"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " # 上述宽表，第0列为因变量，1、2、3列为自变量\n",
    "X = data[[1,2,3]]\n",
    " \n",
    "# ✨✨✨务必注意✨✨✨，一定要加上常数项，如果没有常数项列，计算结果天差地别，可能VIF等于好几千\n",
    "X[4]=1\n",
    "print(X)\n",
    " \n",
    "#计算第2个变量的(第二列)的方差膨胀因子，注意python的计数是从0开始的，所以第二个参数应该是1\n",
    "variance_inflation_factor(X[[1,2,3,4]].values, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "add_constant 等效"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>const</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>149.3</td>\n",
       "      <td>4.2</td>\n",
       "      <td>108.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>161.2</td>\n",
       "      <td>4.1</td>\n",
       "      <td>114.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>171.5</td>\n",
       "      <td>3.1</td>\n",
       "      <td>123.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>175.5</td>\n",
       "      <td>3.1</td>\n",
       "      <td>126.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.0</td>\n",
       "      <td>180.8</td>\n",
       "      <td>1.1</td>\n",
       "      <td>132.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.0</td>\n",
       "      <td>190.7</td>\n",
       "      <td>2.2</td>\n",
       "      <td>137.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1.0</td>\n",
       "      <td>202.1</td>\n",
       "      <td>2.1</td>\n",
       "      <td>146.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1.0</td>\n",
       "      <td>212.1</td>\n",
       "      <td>5.6</td>\n",
       "      <td>154.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1.0</td>\n",
       "      <td>226.1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>162.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.0</td>\n",
       "      <td>231.9</td>\n",
       "      <td>5.1</td>\n",
       "      <td>164.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1.0</td>\n",
       "      <td>239.0</td>\n",
       "      <td>0.7</td>\n",
       "      <td>167.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    const      1    2      3\n",
       "0     1.0  149.3  4.2  108.1\n",
       "1     1.0  161.2  4.1  114.8\n",
       "2     1.0  171.5  3.1  123.2\n",
       "3     1.0  175.5  3.1  126.9\n",
       "4     1.0  180.8  1.1  132.1\n",
       "5     1.0  190.7  2.2  137.7\n",
       "6     1.0  202.1  2.1  146.0\n",
       "7     1.0  212.1  5.6  154.1\n",
       "8     1.0  226.1  5.0  162.3\n",
       "9     1.0  231.9  5.1  164.3\n",
       "10    1.0  239.0  0.7  167.6"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = data[[1,2,3]]\n",
    "X = sm.add_constant(X)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.023478872590032"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# add_constant() 把常数列加到首列，因变量col '1' 变第二列\n",
    "variance_inflation_factor(X.values, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
